from sqlalchemy.orm import DeclarativeBase, relationship
from sqlalchemy import Column, Integer, String, DateTime, Boolean, ForeignKey
from  datetime import datetime
# Base = DeclarativeBase()
class Base(DeclarativeBase):
    pass


class User(Base):
    __tablename__ = "users"

    id = Column(Integer, primary_key=True)
    username = Column(String(255), unique=True, nullable=False)
    nickname = Column(String(255), nullable=False)
    password: str = Column(String(255), nullable=False)
    email: str = Column(String(255), unique=True, nullable=False)
    create_at = Column(DateTime, default=datetime.now())
    def __repr__(self):
        return f"User(id={self.id!r}, name={self.username!r}, nickname={self.nickname!r})"



class ChatRooms(Base):
    __tablename__ = "chat_rooms"
    id = Column(Integer, primary_key=True)
    name: str = Column(String(255), nullable=False, unique=True)
    description = Column(String(255), nullable=False)
    is_public = Column(Boolean, default=0)
    password: str = Column(String(255), nullable=True)
    create_id: int = Column(Integer,ForeignKey("users.id"), nullable=False)
    create_at = Column(DateTime, default=datetime.now())
    def __repr__(self):
        return f"ChatRooms(id={self.id!r}, name={self.name!r}, description={self.description!r})"


class RoomMembers(Base):
    __tablename__ = "room_members"
    id = Column(Integer, primary_key=True)
    room_id: int = Column(Integer, ForeignKey("chat_rooms.id"), nullable=False)
    user_id: int = Column(Integer, ForeignKey("users.id"), nullable=False)
    create_at = Column(DateTime, default=datetime.now())
    join_at = Column(DateTime, default=datetime.now())
    role = Column(String(255), default="member", nullable=False)  # member, admin

class Messages(Base):
    __tablename__ = "messages"
    id = Column(Integer, primary_key=True)
    content = Column(String(255), nullable=False)
    content_type = Column(String(255), default="text", nullable=False)  # text, image
    create_at = Column(DateTime, default=datetime.now())
    room_id: int = Column(Integer, ForeignKey("chat_rooms.id"), nullable=False)
    user_id: int = Column(Integer, ForeignKey("users.id"), nullable=False)
    time_stamp = Column(DateTime, default=datetime.now())   # 时间戳
